package com.siwei.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.siwei.demo.PageUtilRsp;
import com.siwei.demo.model.InputDataReq;
import com.siwei.demo.model.InputDataRsp;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.io.IOException;
import java.net.URL;

@Controller
@RequestMapping("/query")
public class IndexController {

    @RequestMapping(value = "/toPersonList", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String toVideoList() {

        System.out.println("aaaaaaaaa");

        return "/queryPerson";
    }

    @RequestMapping(value = "/getPersonInfo", method = {RequestMethod.GET, RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public PageUtilRsp getPersonInfo(HttpServletRequest request) {
        PageUtilRsp rsp = new PageUtilRsp();
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8099/test/user?wsdl");
        //getUser 为接口中定义的方法名称  张三为传递的参数   返回一个Object数组
        Object[] objects = new Object[0];
        Object obj = null;
        XmlMapper mapper = new XmlMapper();
        InputDataReq req1 = new InputDataReq();
        String patno = request.getParameter("patno");
        String yljgdm = request.getParameter("yljgdm");
        req1.setYljgdm(yljgdm);
        req1.setPatno(patno);
        String xmlStr = null;
        try {
            xmlStr = mapper.writeValueAsString(req1);
            System.out.println(xmlStr);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        Service service = new Service();
        Call call = null;
        try {
            call = (Call) service.createCall();
            //wsdl 的url  不带？wsdl
            call.setTargetEndpointAddress(new URL("http://11.65.9.110:8096/GlobalW/Services/HisService.asmx"));
            call.setOperationName(new QName("http://tempuri.org/", "getWeatherbyCityName"));
            call.addParameter(new QName("http://tempuri.org/", "theCityName"), XMLType.SOAP_VECTOR, ParameterMode.IN);
            call.setReturnType(XMLType.SOAP_VECTOR);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI("http://tempuri.org//getWeatherbyCityName");
            obj = call.invoke(new Object[]{xmlStr});
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回内容  取出list展示到界面
        InputDataRsp rsps = null;
        try {
            rsps = mapper.readValue(obj.toString(), InputDataRsp.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //展示到页面的list
        rsp.setData(rsps.getDoctorInfoList());
        //输出调用结果
        System.out.println("*****" + objects[0].toString());


        return rsp;
    }

    public static void mainaaa(String[] args) {
        String a = "";
    }

    public static void main(String[] args) {
/*        try {
            Client client = new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
//            Object[] results = client.invoke("qqCheckOnline", new String[]{xmlStr});
//            System.out.println(results[0]);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }
}
